from django.core.paginator import Paginator, EmptyPage,PageNotAnInteger
from django.shortcuts import render, get_object_or_404
# 导⼊发送邮件函数
from django.core.mail import send_mail
from .models import Post
from .forms import EmailPostForm, CommentForm,SearchForm
# 导⼊标签类
from taggit.models import Tag
# 导入Django聚合函数Count
from django.db.models import Count 
# 导入Django提供的搜索向量类
from django.contrib.postgres.search import SearchVector

def post_list(request,tag_slug=None):
  posts = Post.published.all() 
  # 创建变量tag，⽤于存储下⽅代码获取的标签
  tag = None
 # 如果请求中有tag_slug，则获取标签并按标签过滤⽂章数据
  if tag_slug:
 # 获取标签
      tag = get_object_or_404(Tag, slug=tag_slug)
 # 按标签过滤⽂章
      posts = posts.filter(tags=tag)
  paginator = Paginator(posts, 3)
  page = request.GET.get('page',1)
  try:
    posts = paginator.page(page)
  except PageNotAnInteger:
    posts = paginator.page(1)
  except EmptyPage:
    posts = paginator.page(paginator.num_pages),
  return render(request,'blog/post/list.html', {'posts' : posts, 'page': page,'tag': tag})


def post_detail(request, year, month, day, post):
  post = get_object_or_404(Post, slug=post, status='published',
                          publish__year=year,
                          publish__month=month,
                          publish__day=day)
  # 获取数据库中当前⽂章的所有评论中状态为可以查看的评论
  comments = post.comments.filter(active=True)                        
  # 创建⼀个新评论变量，默认为None
  new_comment = None
 # 如果请求类型是POST，则获取请求中的评论数据构建评论并保存到数据库
  if request.method == 'POST':
 # 获取请求中的评论数据构建评论表单
      comment_form = CommentForm(data=request.POST)
 # 如果通过表单有效性校验则构建评论数据并保存
      if comment_form.is_valid():
          new_comment = comment_form.save(commit=False)
          new_comment.post = post
          new_comment.save()
  else:
 # 如果请求类型不是POST，则构建空评论表单   


# 创建评论表单实例
    comment_form = CommentForm() 
  post_tags_ids = post.tags.values_list('id', flat=True)
# 找到包含当前⽂章任意标签的⽂章集合（除当前⽂章外）
  similar_posts = Post.published.filter(tags__in=post_tags_ids)\
                                  .exclude(id=post.id)
# 对相似⽂章按照相同标签数量进⾏排序，取前4个，相同标签数量使⽤聚合函数Count
# 计算后保存在same_tags中，以便后续使⽤
  similar_posts = similar_posts.annotate(same_tags=Count('tags'))\
                              .order_by('-same_tags','-publish')[:4]    
  return render(request,
                  'blog/post/detail.html',
                  {'post': post,
                  'comments': comments,
                  'new_comment': new_comment,
                  'comment_form': comment_form,
                  'similar_posts': similar_posts})# 此处为相似⽂章

def post_share(request, post_id):
  # 获取要分享的⽂章信息
  post = get_object_or_404(Post, id=post_id, status='published')
  sent = False
 # 如果请求是POST表示有提交表单
  if request.method == 'POST':
 # 通过EmailPostForm表单类获取请求中的表单数据后保存到form中
      form = EmailPostForm(request.POST)
 # 如果表单数据验证通过则发送邮件并将状态设置为已发送
      if form.is_valid():
          cd = form.cleaned_data
 # 构建请求路径
          post_url = request.build_absolute_uri(post.get_absolute_url())
 # 构建邮件标题
          subject = f"{cd['name']} recommends you read " \
                      f"{post.title}"
 # 构建邮件正⽂
          message = f"Read {post.title} at {post_url}\n\n" \
                      f"{cd['name']}\'s comments: {cd['comments']}"
 # 发送邮件
          send_mail(subject, message, '3067044383@qq.com',
                      [cd['to']])
        # 发送邮件，稍后完成
         # 发送状态设置为真
          sent = True
  else:
      # 如果表单数据验证未通过将表单设置为空
      form = EmailPostForm()    
      #渲染分享文章模板，并将表单，文章发送状态信息传递给文章分享模板
  return render(request, 'blog/post/share.html',{'post': post,'form': form, 'sent': sent})
def post_search(request):
    form = SearchForm()
    query = None
    results = []
 # 如果请求中有query字段则获取表单中的数据进行处理
    if 'query' in request.GET:
 # 获取表单
        form = SearchForm(request.GET)
 # 如果表单中的数据验证通过，即有效，则获取并处理
        if form.is_valid():
 # 获取搜索内容保存下来
            query = form.cleaned_data['query']
 # 根据标题和正文字段构建搜索向量并生成搜索字段，再根据搜索内容筛选出匹配内容保存
            results = Post.published.annotate(
              search=SearchVector('title', 'body')
            ).filter(search=query)
 # 将搜索结果渲染到搜索模板并返回
    return render(request, 
                  'blog/post/search.html', {
                  'form': form,
                  'query': query,
                  'results': results
                  })